//
//  LWSelectProjCtrl.m
//  laowu
//
//  Created by 尹桥印 on 15/5/1.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "LWSelectProjCtrl.h"
#import "ProjectCell.h"
#import "ProjectDataTool.h"


static NSString * const reuseIdentifier = @"ProjectCell";
@interface LWSelectProjCtrl ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,weak)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *projectInfoArray;
@end

@implementation LWSelectProjCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    self.projectInfoArray = [NSMutableArray array];
    self.navigationItem.title = @"选择项目";
    [self initCollectionView];
    [self getDataWithMaxId:0 feature:0];
}

-(void)getDataWithMaxId:(NSInteger)maxId feature:(NSInteger)feature{
    [ProjectDataTool getProjectInfoData:maxId feature:feature keywords:@"" success:^(ProjectInfoRes *result) {
        if(result.isSucc){
            [self.projectInfoArray addObjectsFromArray:result.datas];
            [self.collectionView reloadData];
        }else{
            [DialogUtils showTipsWithView:self.view String:result.msg];
        }
    } failure:^(NSError *error) {
        [DialogUtils showTipsWithView:self.view String:@"加载失败"];
    }];
}

-(void)initCollectionView{
    UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc]init];
    [flowLayout setMinimumLineSpacing:5];
    [flowLayout setMinimumInteritemSpacing:5];
    UICollectionView *collectionView  =[[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    collectionView.height = collectionView.height-49;
    
    [collectionView setBackgroundColor:RGBColor(240, 240, 240)];
    [collectionView registerClass:[ProjectCell class] forCellWithReuseIdentifier:@"collectionView"];
    
    [collectionView setDelegate:self];
    [collectionView setDataSource:self];
    [collectionView setAlwaysBounceVertical:YES];
    [collectionView registerNib:[UINib nibWithNibName:reuseIdentifier bundle: [NSBundle mainBundle]] forCellWithReuseIdentifier:reuseIdentifier];
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.projectInfoArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ProjectCell *cell = [ProjectCell cellWithCollectionView:collectionView indexPath:indexPath];
    [cell initData:self.projectInfoArray[indexPath.row]];
    [cell setBackgroundColor:[UIColor whiteColor]];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SCREENW-15)/2, 200);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ProjectInfoResult *result = self.projectInfoArray[indexPath.row];
    if ([self.delegate respondsToSelector:@selector(lwSelectProjectItemClick:)]) {
        [self.delegate lwSelectProjectItemClick:result];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    NSLog(@"indexpath.row=%ld",indexPath.row);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5,5, 5);
}


-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

@end
